<?php
include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/model/config.php");
if(!isset($_SESSION['username'])) {
	header("Location: ".$cfg['basepath']."/ObstacleWebApp/index.php");
}
require('../../fpdf.php');

$query="SELECT * FROM screeninguser, user where screeninguser.UserId=user.Id and screeninguser.ScreeningId='" .$_GET["id"]."'";
$result = mysql_query($query) or die ("FOUT: " . mysql_error());

// gegevens ophalen en toekennen aan tijdelijke variabelen
while ($rij = mysql_fetch_array($result)){
        $UserName = $rij['FirstName'] . " " . $rij['LastName'];
}

class PDF extends FPDF
{
// Page header
function Header()
{
    if ( $this->PageNo() !== 1 ) {
        // Logo
        $size="30"; //(min 0, max 210; it's for making image big or small,nothing else)
        $absx=(210-$size)/2;
        $this->Image('../../images/headerpdf.png',$absx,5,$size);
        // Line break
        $this->Ln(30);
    }
    
}

// Page footer
function Footer()
{
    if ( $this->PageNo() !== 1 ) {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Horizontal Line
        $this->Cell(0,0,'',1,1,'C');
        // Page number
        $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
    }
}
}

// Instanciation of inherited class
$pdf = new PDF('P','mm','A4');
$pdf->AliasNbPages();
$pdf->AddPage();

// Logo
$pdf->Image('../../images/logo_voorblad.png',50,40,110);
$pdf->Ln(140);

// Report Name
$pdf->SetFont( 'Arial', 'BU', 28 );
$pdf->Cell( 0, 15, 'Screeningsrapport', 0, 0, 'C' );
$pdf->Ln();

// Screener name
$pdf->SetFont( 'Arial', 'BU', 20 );
$pdf->Cell( 0, 15, 'Gescreened door:', 0, 0, 'C' );
$pdf->Ln();
$pdf->SetFont( 'Arial', '', 16 );
$pdf->Cell( 0, 15, $UserName, 0, 0, 'C' );
$pdf->Ln();

// Patient info

$query5 = "SELECT * FROM screening WHERE Id=".$_GET["id"]."";
$result5 = mysql_query($query5) or die (mysql_error());
while ($row5 = mysql_fetch_array($result5))
{
    $query6 = "SELECT * FROM patient where Id=".$row5['PatientId']."";
    $result6 = mysql_query($query6) or die (mysql_error());
    while ($row6 = mysql_fetch_array($result6))
    {
        $FirstName = $row6['FirstName'];
        $LastName = $row6['LastName'];
        $BuildingId = $row6['BuildingId'];
        
        $query7 = "SELECT * FROM building WHERE Id=".$BuildingId."";
        $result7 = mysql_query($query7) or die (mysql_error());
        while ($row7 = mysql_fetch_array($result7))
        {
            $Address = $row7['Street']." ".$row7['Number'];
            $CityId = $row7['CityId'];
            
            $query8 = "SELECT * FROM city WHERE Id=".$CityId."";
            $result8 = mysql_query($query8) or die (mysql_error());
            while ($row8 = mysql_fetch_array($result8))
            {
                $Zip_City = $row8['ZIP']." ".$row8['City'];
            }
        }
    }
}


$pdf->SetFont( 'Arial', 'BU', 20 );
$pdf->Cell( 0, 15, iconv("UTF-8", "ISO-8859-1", 'Patiënten info:'), 0, 0, 'C' );
$pdf->Ln();
$pdf->SetFont( 'Arial', '', 16 );
// Name
$pdf->Cell( 0, 15, iconv("UTF-8", "ISO-8859-1", $FirstName." ".$LastName), 0, 0, 'C' );
$pdf->Ln();
// Address
$pdf->Cell( 0, 15, iconv("UTF-8", "ISO-8859-1", $Address), 0, 0, 'C' );
$pdf->Ln();
// Zip-code + area
$pdf->Cell( 0, 15, iconv("UTF-8", "ISO-8859-1", $Zip_City), 0, 0, 'C' );
$pdf->Ln();


$pdf->AddPage();


$query1="SELECT * FROM screeningroom, roomtype where roomtype.Id=screeningroom.RoomTypeId and screeningroom.ScreeningVersionId='" .$_GET["id"]."'";
$result1 = mysql_query($query1) or die ("FOUT1: " . mysql_error());
$num_rows = mysql_num_rows($result1);
$current_row = 0;
// gegevens ophalen en toekennen aan tijdelijke variabelen
while ($rij1 = mysql_fetch_array($result1)){
        $RoomTypeId = $rij1['RoomTypeId']; 
        $RoomName = $rij1['Name'];
        
        //Kamer
        $pdf->SetFont('Times','B',14);
        $pdf->Cell(0,7,$RoomName,0,0,'C');
        $pdf->Ln();
        // Horizontal Line
        $pdf->Cell(0,0,'',1,1,'C');
        $pdf->Ln(5);

        $current_row++;
        
        $query2="SELECT * FROM screeningitem, roomitem where roomitem.Id=screeningitem.RoomItemId and screeningitem.ScreeningRoomId='" .$RoomTypeId."'";
        $result2 = mysql_query($query2) or die ("FOUT1: " . mysql_error());

        // gegevens ophalen en toekennen aan tijdelijke variabelen
        while ($rij2 = mysql_fetch_array($result2)){
                $RoomItemId = $rij2['RoomItemId']; 
                $ItemName = $rij2['Name'];
                $ProblemComment = $rij2['Comment'];

                //Item
                $pdf->SetFont('Times','B',13);
                $pdf->Cell(12,5,$ItemName);
                $pdf->Ln();
                
                
                $query3="SELECT itemproblem.Id, itemproblem.Name, screeningproblem.Value FROM screeningproblem, itemproblem where itemproblem.Id=screeningproblem.ItemProblemId and screeningproblem.ScreeningItemId='" .$RoomItemId."'";
                $result3 = mysql_query($query3) or die ("FOUT1: " . mysql_error());

                // gegevens ophalen en toekennen aan tijdelijke variabelen
                while ($rij3 = mysql_fetch_array($result3)){
                    $ItemId = $rij3['Id'];
                    $ProblemName = $rij3['Name'];   
                    $ProblemValue = $rij3['Value'];
                    
                    //Item
                    $pdf->SetFont('Times','',12);
                    $pdf->SetX(20);
                    $pdf->Cell(12,5,$ProblemName);
                    $pdf->Ln();
                    
                    $query4="SELECT * from itemproblem where Id='" .$ItemId."'";
                    $result4 = mysql_query($query4) or die ("FOUT4: " . mysql_error());

                    // gegevens ophalen en toekennen aan tijdelijke variabelen
                    while ($rij4 = mysql_fetch_array($result4)){
                        if ($rij4['HasValue'] == "T")
                        {
                            $pdf->SetX(30);
                            $pdf->Cell(12,5,'Value:' . $ProblemValue);
                            $pdf->Ln();
                        }
                    }
                }
                
                $pdf->SetFont('Times','',12);
                $pdf->Cell(12,5,'Comment:' . $ProblemComment);
                $pdf->Ln();
        }
        if ($num_rows>$current_row)
        {
           $pdf->AddPage(); 
        }
        
}




//$pdf->Ln();


$pdf->Output("screening".$_GET["id"].".pdf", "D");
?>